package com.navcoo.yidaaddproapi.config;


import com.navcoo.yidaaddproapi.response.Result;
import com.navcoo.yidaaddproapi.response.exception.ResultStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常处理程序
 *
 * @author WY
 * @date 2024/01/26
 */
@ControllerAdvice
public class MyExceptionHandler {

    /**
     * 方法参数不合法异常
     *
     * @param e e
     * @return {@link Result}<{@link String}>
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        return Result.failure(ResultStatus.ERROR_PARAM, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

}
